package cn.fl.spl.service.impl;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.spl.service.ISplRenewProtocolRecService;
import cn.fl.spl.transform.SplRenewProtocolRecVOTransform;
import cn.fl.spl.vo.SplRenewProtocolRecVO;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.spl.api.ISplRenewProtocolRecApiService;
import fl.spl.facade.spl.condition.SplRenewProtocolRecQC;
import fl.spl.facade.spl.dto.SplRenewProtocolRecDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by jiangzhongge on 2018-7-10.
 * Time 13:55.
 * Class belongs to fl-ass-service.
 */
@Service
public class SplRenewProtocolRecServiceImpl implements ISplRenewProtocolRecService {

    @Autowired
    private ISplRenewProtocolRecApiService splRenewProtocolRecApiService;

    @Override
    public Long addSplRePtlRecVO(SplRenewProtocolRecVO entity) throws BaseException {
        if(null == entity){
            BaseLogUtils.newWebLogger("新增续签协议对象不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
        }
        return this.splRenewProtocolRecApiService.addSplRePtlRecDTO(SplRenewProtocolRecVOTransform.toDTO(entity));
    }

    @Override
    public boolean removeSplRePtlRecVOByIds(List<Long> ids, Long mdfUsrId) throws BaseException {
        if(CollectionUtils.isEmpty(ids)){
            BaseLogUtils.newWebLogger("删除经销商续签协议记录不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
        }
        return this.splRenewProtocolRecApiService.removeSplRePtlRecDTOByIds(ids, mdfUsrId);
    }

    @Override
    public boolean modifySplRePtlRecVO(SplRenewProtocolRecVO entity) throws BaseException {
        if(null == entity){
            BaseLogUtils.newWebLogger("修改经销商续签协议记录不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
        }
        return this.splRenewProtocolRecApiService.modifySplRePtlRecDTO(SplRenewProtocolRecVOTransform.toDTO(entity));
    }

    @Override
    public boolean updateSplRePtlRecVOByQC(SplRenewProtocolRecQC condition, Map<String, String> mdfMap) throws BaseException {
        if(null == condition){
            BaseLogUtils.newWebLogger("通过条件修改经销商续签协议记录，条件不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
        }
        if(CollectionUtils.isMapEmpty(mdfMap)){
            BaseLogUtils.newWebLogger("通过条件修改经销商续签协议记录，修改信息不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
        }
        return this.splRenewProtocolRecApiService.updateSplRePtlRecDTOByQC(condition, mdfMap);
    }

    @Override
    public SplRenewProtocolRecVO selectSplRePtlRecVOById(Long id) throws BaseException {
        if(StringUtils.isEmpty(id)){
            BaseLogUtils.newWebLogger("查询经销商续签协议记录，主键不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
        }
        return SplRenewProtocolRecVOTransform.toVO(this.splRenewProtocolRecApiService.selectSplRePtlRecDTOById(id));
    }

    @SuppressWarnings("unchecked")
	@Override
    public Pager searchSplRePtlRecVOByPage(Pager page) throws BaseException {
        if(null == page){
            BaseLogUtils.newWebLogger("分页查询经销商续签协议记录，分页参数不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
        }
        Pager pager = this.splRenewProtocolRecApiService.searchSplRePtlRecDTOByPage(page);
        List<SplRenewProtocolRecDTO> resultList = (List<SplRenewProtocolRecDTO>) pager.getResultList();
        pager.setResultList(SplRenewProtocolRecVOTransform.toVOList(resultList));
        return pager;
    }

    @Override
    public List<SplRenewProtocolRecVO> searchSplRePtlRecVOList(SplRenewProtocolRecQC condition) throws BaseException {
        if(null == condition){
            BaseLogUtils.newWebLogger("通过条件查询经销商续签协议记录，条件不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
        }
        return SplRenewProtocolRecVOTransform.toVOList(this.splRenewProtocolRecApiService.searchSplRePtlRecDTOList(condition));
    }

	@Override
	public DataResultVO addRenewProtocolFlow(SplRenewProtocolRecVO vo, String buOrgCd, String sysCd, String orgCd) throws BaseException {
		String methodName = "addRenewProtocolFlow";
		DataResultVO result = new DataResultVO();
		if (null == vo) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("实体类为空");
            BaseLogUtils.newWebLogger("实体类为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
			return result;
		}
		if (StringUtils.isEmpty(buOrgCd)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("所属事业部不能空");
            BaseLogUtils.newWebLogger("所属事业部为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
			return result;
		}
		if (StringUtils.isEmpty(sysCd)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统代码不能为空");
            BaseLogUtils.newWebLogger("系统代码不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
			return result;
		}
		if (StringUtils.isEmpty(orgCd)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("组织架构代码不能为空");
            BaseLogUtils.newWebLogger("组织架构代码不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
			return result;
		}
		DataResultDTO dtoResult = this.splRenewProtocolRecApiService.addRenewProtocolFlow(SplRenewProtocolRecVOTransform.toDTO(vo), 
				buOrgCd, sysCd, orgCd);
		if ("1".equals(dtoResult.getCode())) {
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo(dtoResult.getInfo());
		}
		if ("0".equals(dtoResult.getCode())) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(dtoResult.getInfo());
		}
		return result;
	}
}